package com.wangnian.wangcai;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.wangnian.wangcai.dao.DayInfo;
import com.wangnian.wangcai.util.DateUtil;
import com.wangnian.wangcai.util.Indicators;

import java.util.ArrayList;
import java.util.List;

public class test {

    public static void main(String[] args) {
        //根据 name 更新所有的行情数据
        String endDateStr = DateUtil.subDay(0, DateUtil.YYYYMMDD);
        String startDateStr = DateUtil.subDay(40, DateUtil.YYYYMMDD);
        List<DayInfo> dayInfoList = new ArrayList<>(400);
        String codeStr;
        codeStr = "1.000001";
        String path = "http://push2his.eastmoney.com/api/qt/stock/kline/get?secid=" + codeStr + "&fields1=f1%2Cf2%2Cf3%2Cf4%2Cf5&fields2=f51%2Cf52%2Cf53%2Cf54%2Cf55%2Cf56%2Cf57%2Cf58&klt=101&fqt=1&beg=" + startDateStr + "&end=" + endDateStr;
        String str = HttpRequest.get(path).execute().body();
        JSONObject jsonObject = JSONObject.parseObject(str);
        JSONObject data = jsonObject.getJSONObject("data");
        if (data == null) {
            return;
        }
        JSONArray jsonArray = data.getJSONArray("klines");
        if (jsonArray.size() < 3) {
            return;
        }

        for (Object string : jsonArray) {
            String str1 = (String) string;
            String[] s1 = str1.split(",");
            DayInfo dayInfo = new DayInfo();
            dayInfo.setCode(codeStr);
            dayInfo.setTime(DateUtil.parseToDate(s1[0], DateUtil.DATEFORMAT_YYYYMMDD));
            dayInfo.setOpen(Double.parseDouble(s1[1]));
            dayInfo.setClose(Double.parseDouble(s1[2]));
            dayInfo.setHigh(Double.parseDouble(s1[3]));
            dayInfo.setLow(Double.parseDouble(s1[4]));
            dayInfo.setVolume(Double.parseDouble(s1[5]));
            dayInfoList.add(dayInfo);
        }

        // 计算KDJ指标（同花顺/通达信算法）
        double[] kdj = Indicators.calculateKDJ_THS(dayInfoList);
        
        // 打印最近10天的KDJ值
        System.out.println(String.format("最后一天KDJ值 (9,3,3): K=%.2f, D=%.2f, J=%.2f", 
            kdj[0], kdj[1], kdj[2]));
    }
    
  
}
